package org.needcoke.hs.mp.importer;

import cn.hutool.core.bean.BeanUtil;
import org.needcoke.hs.config.ConfigImporter;
import org.needcoke.hs.config.InterceptorPropertiesConfig;
import org.needcoke.hs.mp.bean.Config;
import org.needcoke.hs.mp.bean.InterceptRule;
import org.needcoke.hs.mp.bean.SingleUrl;
import org.needcoke.hs.mp.bean.UrlGroup;
import org.needcoke.hs.mp.mapper.ConfigMapper;
import org.needcoke.hs.mp.mapper.InterceptRuleMapper;
import org.needcoke.hs.mp.mapper.SingleUrlMapper;
import org.needcoke.hs.mp.mapper.UrlGroupMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MybatisPlusImporter implements ConfigImporter {

    @Resource
    private InterceptorPropertiesConfig signatureBeanConfig;

    @Autowired
    private ConfigMapper configMapper;

    @Autowired
    private InterceptRuleMapper interceptRuleMapper;

    @Autowired
    private SingleUrlMapper singleUrlMapper;

    @Autowired
    private UrlGroupMapper urlGroupMapper;

    @Override
    public void importConfig() {
        List<Config> configs = configMapper.selectList(null);

        List<InterceptRule> interceptRules = interceptRuleMapper.selectList(null);
        List<SingleUrl> singleUrls = singleUrlMapper.selectList(null);
        for (SingleUrl singleUrl : singleUrls) {
            singleUrl.setInterceptRule(new InterceptRule().setAgreement(singleUrl.getAgreement())
                    .setHost(singleUrl.getHost())
                    .setPath(singleUrl.getPath())
                    .setPort(singleUrl.getPort())
            );

        }
        List<UrlGroup> urlGroups = urlGroupMapper.selectList(null);
        List<org.needcoke.hs.bean.Config> cfs = new ArrayList<>();
        configs.forEach((config) -> {
            org.needcoke.hs.bean.Config cf = new org.needcoke.hs.bean.Config();
            BeanUtils.copyProperties(config, cf);
            cfs.add(cf);
        });

        List<org.needcoke.hs.bean.SingleUrl> sus = new ArrayList<>();
        singleUrls.forEach((singleUrl) -> {
            org.needcoke.hs.bean.SingleUrl su = new org.needcoke.hs.bean.SingleUrl();
            BeanUtil.copyProperties(singleUrl, su);
            sus.add(su);
        });

        signatureBeanConfig.getConfig().addAll(cfs);
        signatureBeanConfig.getSingleUrl().addAll(sus);


        Map<String, List<org.needcoke.hs.bean.InterceptRule>> interceptRuleMap = new HashMap<>();
        for (InterceptRule interceptRule : interceptRules) {
            if (!interceptRuleMap.containsKey(interceptRule.getUrlGroupName())) {
                interceptRuleMap.put(interceptRule.getUrlGroupName(), new ArrayList<>());
            }
            org.needcoke.hs.bean.InterceptRule ir = new org.needcoke.hs.bean.InterceptRule();
            BeanUtil.copyProperties(interceptRule,ir);
            interceptRuleMap.get(interceptRule.getUrlGroupName()).add(ir);
        }
        List<org.needcoke.hs.bean.UrlGroup> ugs = new ArrayList<>();
        urlGroups.forEach((urlGroup) -> {
            org.needcoke.hs.bean.UrlGroup ug = new org.needcoke.hs.bean.UrlGroup();
            BeanUtil.copyProperties(urlGroup, ug);
            ug.setInterceptRules(interceptRuleMap.get(urlGroup.getName()));
            ugs.add(ug);
        });
        signatureBeanConfig.getUrlGroup().addAll(ugs);
    }
}
